package com.xiaobawang.hundouluo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentTabHost;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xiaobawang.hundouluo.fragment.DataFragment;
import com.xiaobawang.hundouluo.fragment.EnvironmentFragment;
import com.xiaobawang.hundouluo.fragment.EquipmentFragment;
import com.xiaobawang.hundouluo.fragment.SettingFragment;
import com.xiaobawang.hundouluo.utils.SharedPreferencesUtil;



public class IndexActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private FragmentTabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        initToolBar();
        initTabhost();
//        current_fragment = fragmentManager.findFragmentById(id);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_menu , menu);
        return super.onCreateOptionsMenu(menu);
    }

    public void initToolBar() {
        // 如果要修改toolbar标题栏文字，可添加如下语句
        //toolbar.setTitle(getIntent().getStringExtra("loginName"));
        // toolbar.setTitle("智慧农业") ;
        toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle(getIntent().getStringExtra("userName"));
        setSupportActionBar(toolbar);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.toolbar_logout :
                        SharedPreferencesUtil.clear(IndexActivity.this, "userInfo");
                        Intent intent = new Intent(IndexActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                        break;
                    case R.id.toolbar_exit :
                        new AlertDialog.Builder(IndexActivity.this)
                                .setTitle("确认退出？")
                                .setNegativeButton("取消", null)
                                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        finish();
                                    }
                                })
                                .show();
                        break;
                }
                return false;
            }
        }) ;
    }

    private void initTabhost() {
        // 设置不同的页面的区域
        tabHost = findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), R.id.realcontent);
        // 为FragmentTabHost设置标签标题和对应的标签页
        tabHost.addTab(
                tabHost.newTabSpec("Environment").setIndicator(setTabContent("实时环境", R.drawable.footer_environment_selector))
                , EnvironmentFragment.class // 设置点击该标签时加载的Fragment类
                , null// Fragment初始化时如果需要传参，通过该参数传递
        );
        tabHost.addTab(
                tabHost.newTabSpec("Equipment").setIndicator(setTabContent("设备控制", R.drawable.footer_equipment_selector))
                , EquipmentFragment.class // 设置点击该标签时加载的Fragment类
                , null// Fragment初始化时如果需要传参，通过该参数传递
        );
        tabHost.addTab(
                tabHost.newTabSpec("Data").setIndicator(setTabContent("数据分析", R.drawable.footer_data_selector))
                , DataFragment.class // 设置点击该标签时加载的Fragment类
                , null// Fragment初始化时如果需要传参，通过该参数传递
        );
        tabHost.addTab(
                tabHost.newTabSpec("Setting").setIndicator(setTabContent("参数设置", R.drawable.footer_setting_selector))
                , SettingFragment.class // 设置点击该标签时加载的Fragment类
                , null// Fragment初始化时如果需要传参，通过该参数传递
        );
        tabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
    }

    public View setTabContent(String label, int imageId) {
        //引入选项卡的布局
        View view = LayoutInflater.from(this).inflate(R.layout.tabcontent, null);
        TextView textView = view.findViewById(R.id.footer_tv);
        textView.setText(label);
        ImageView imageView = view.findViewById(R.id.footer_iv);
        imageView.setImageResource(imageId);
        return view;
    }



}
